package com.qymerge.manage.system.dao.impl;

import com.qymerge.manage.core.dao.BaseDao;
import com.qymerge.manage.system.ro.SysMenuRoleRO;
import com.qymerge.manage.system.ro.SysRoleRO;
import com.qymerge.manage.system.dao.ISysMenuRoleDao;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 *<p>Title: SysMenuRoleDao</p>
 *<p>Description: </p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/7/27/0027
 *@Time 11:09
 */
@Repository
public class SysMenuRoleDaoImpl extends BaseDao implements ISysMenuRoleDao {

    /**
     * 菜单列表
     *
     * @param sysRole
     * @return
     */
    @Override
    public List<SysMenuRoleRO> list(SysRoleRO sysRole) {
        return null;
    }

    /**
     * 获取所有角色的菜单
     *
     * @param roleIds
     * @return
     */
    @Override
    public List<SysMenuRoleRO> findAllByIds(Set<Long> roleIds) {
        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(this.getJdbcTemplate());
        String sql = "select * from dwd_MCRS_MDM_sys_menu_role where 1=1";

        // 参数封装roleIds
        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (null != roleIds) {
            if (null != roleIds && roleIds.size() > 0) {
                sql += " and role_id in (:param) ";
                paramMap.put("param", roleIds);
            }
        }

        return jdbc.query(sql, paramMap, new RowMapper<SysMenuRoleRO>() {

            @Override
            public SysMenuRoleRO mapRow(ResultSet rs, int index) throws SQLException {
                SysMenuRoleRO ro = new SysMenuRoleRO();
                ro.setId(rs.getLong("id"));
                ro.setMenu_id(rs.getLong("menu_id"));
                ro.setRole_id(rs.getLong("role_id"));
                ro.setCreate_user_id(rs.getLong("create_user_id"));
                ro.setCreate_time(rs.getLong("create_time"));
                ro.setStatus(rs.getInt("status"));
                return ro;
            }

        });
    }

    /**
     * 判断菜单是否被角色引用
     *
     * @param menuId
     * @return
     */
    @Override
    public boolean ifExists(Long menuId, Long roleId) {
        String sql = "select count(1) from dwd_MCRS_MDM_sys_menu_role where 1=1 and menu_id=? and role_id=?";
        try {
            return getJdbcTemplate().queryForObject(sql, Integer.class, menuId, roleId) > 0;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     *
     * @param ro
     * @return
     */
    @Override
    public int install(SysMenuRoleRO ro) {
        String sql = "insert into dwd_MCRS_MDM_sys_menu_role(menu_id, role_id, create_user_id, create_time, status) values(?,?,?,?,?)";
        return getJdbcTemplate().update(sql, ro.getMenu_id(), ro.getRole_id(), ro.getCreate_user_id(), ro.getCreate_time(), ro.getStatus());
    }

    /**
     * 批量增加
     *
     * @param list
     */
    @Override
    public void installBatch(List<SysMenuRoleRO> list) {
        String sql="insert into dwd_MCRS_MDM_sys_menu_role(menu_id, role_id, create_user_id, create_time, status)" + " values(?,?,?,?,?)";

        getJdbcTemplate().batchUpdate(sql,new BatchPreparedStatementSetter() {

            @Override
            public int getBatchSize() {
                return list.size();
            }

            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setLong(1, list.get(i).getMenu_id());
                ps.setLong(2, list.get(i).getRole_id());
                ps.setLong(3, list.get(i).getCreate_user_id());
                ps.setLong(4, list.get(i).getCreate_time());
                ps.setInt(5, list.get(i).getStatus());
            }
        });
    }

    /**
     * 删除菜单
     *
     * @param menuId 菜单ID
     * @param roleId 角色ID
     * @return
     */
    @Override
    public int remove(Long menuId, Long roleId) {
        //删除
        String sql="delete from dwd_MCRS_MDM_sys_menu_role where menu_id=? and role_id=?";
        return getJdbcTemplate().update(sql, menuId, roleId);
    }

}


